package com.example;

/**
 * Created by IntelliJ IDEA.
 * User: Tomek
 * Date: 06.05.11
 * Time: 13:14
 * To change this template use File | Settings | File Templates.
 */

	import android.content.Context;
	import android.database.Cursor;
	import android.database.sqlite.SQLiteDatabase;
	import android.database.sqlite.SQLiteOpenHelper;
	import android.database.sqlite.SQLiteStatement;
    import android.os.DropBoxManager;
    import android.text.format.DateUtils;
    import android.text.method.DateTimeKeyListener;
    import android.util.Log;

	import java.util.ArrayList;
	import java.util.List;

	public class DataHelper {

	   private static final String DATABASE_NAME = "android2.db";
	   private static final int DATABASE_VERSION = 1;
	   private static final String TABLE_NAME = "tasks2";
       private static final String SYNCHRONIZED_DATE = "date";

	   private Context context;
	   private SQLiteDatabase db;

	   private SQLiteStatement insertStmt;
	   private static final String INSERT = "insert into "
	      + TABLE_NAME + "(id_task, task_name, task_where, task_description, task_from, task_to) values (?, ?, ?, ?, ?, ?);";

	   public DataHelper(Context context) {
	      this.context = context;
	      OpenHelper openHelper = new OpenHelper(this.context);
	      this.db = openHelper.getWritableDatabase();
	      this.insertStmt = this.db.compileStatement(INSERT);
	   }

	   public long insert(String id_task, String task_name, String task_where, String task_description, String task_from, String task_to) {
	      this.insertStmt.bindString(1, id_task);
          this.insertStmt.bindString(2, task_name);
          this.insertStmt.bindString(3, task_where);
          this.insertStmt.bindString(4, task_description);
          this.insertStmt.bindString(5, task_from);
          this.insertStmt.bindString(6, task_to);
	      return this.insertStmt.executeInsert();
	   }

	   public void deleteAll() {
	      this.db.delete(TABLE_NAME, null, null);
	   }

	   public ArrayList<Task> selectDate(String data) {
           /*
            */
           ArrayList<Task> list = new ArrayList<Task>();
	      //List<String> list = new ArrayList<String>();
	      Cursor cursor = this.db.query(TABLE_NAME, new String[] { "id_task","task_name","task_where","task_description","task_from","task_to" },
                  "id_task='" + data + "'", null, null, null, "task_name asc");
	      if (cursor.moveToFirst()) {
	         do {
	            list.add(new Task(cursor.getString(0), cursor.getString(1), cursor.getString(2),cursor.getString(4),cursor.getString(5),cursor.getString(3)));
	         } while (cursor.moveToNext());
	      }
	      if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
	      }
	      return list;
	   }

        public List<Task> selectAll() {
           /*
            */
           List<Task> list = new ArrayList<Task>();
	      //List<String> list = new ArrayList<String>();
	      Cursor cursor = this.db.query(TABLE_NAME, new String[] { "task_name","id_task" },
                  null, null, null, null, "task_name desc");
	      if (cursor.moveToFirst()) {
	         do {
	            list.add(new Task(cursor.getString(1),cursor.getString(0),null,null,null,null));
	         } while (cursor.moveToNext());
	      }
	      if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
	      }
	      return list;
	   }

	   private static class OpenHelper extends SQLiteOpenHelper {

	      OpenHelper(Context context) {
	         super(context, DATABASE_NAME, null, DATABASE_VERSION);
	      }

	      @Override
	      public void onCreate(SQLiteDatabase db)
          {

              db.execSQL("CREATE TABLE " + TABLE_NAME +
             "(id_list INTEGER AUTO_INCREMENT PRIMARY KEY, " +
                     "id_task String, " +
                     "task_name String, " +
                     "task_where String, " +
                     "task_description String, " +
                     "task_from String, " +
                     "task_to String);");
	      }

	      @Override
	      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
	         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
	         onCreate(db);
	      }
	   }
	}
